﻿using ORS.OrderRobot.Cache;
using ORS.OrderRobot.IOC;

namespace ORS.OrderRobot.Service
{
	public class LockCacheManager: ISingleton
    {
        private readonly ICache _cache;
        public LockCacheManager(ICache cache)
		{
			_cache = cache;
		}

        #region 登录锁
        public Task<string?> GetLoginLock(string email)
        {
            var lockKey = Consts.CacheKey.GenLoginLockKey(email);
            return _cache.GetAsync<string>(lockKey);
        }
        public async Task SetLoginLock(string email,string id, int expiredMillionSecond)
        {
            var lockKey = Consts.CacheKey.GenLoginLockKey(email);
            await _cache.SetAsync(lockKey,id, TimeSpan.FromMilliseconds(expiredMillionSecond));
        }
        public async Task RemoveLoginLock(string email)
        {
            var lockKey = Consts.CacheKey.GenLoginLockKey(email);
            await _cache.RemoveAsync(lockKey);
        }
        #endregion

        #region 设置下单依赖锁
        public Task<string?> GetShopDepLock(string email)
		{
            var lockKey = Consts.CacheKey.GenShopDepLockKey(email);
            return _cache.GetAsync<string>(lockKey);
        }
        public async Task SetShopDepLock(string email,string value,int expiredMillionSecond)
        {
            var lockKey = Consts.CacheKey.GenShopDepLockKey(email);
            await _cache.SetAsync(lockKey,value, TimeSpan.FromMilliseconds(expiredMillionSecond));
        }
        public async Task RemoveShopDepLock(string email)
        {
            var lockKey = Consts.CacheKey.GenShopDepLockKey(email);
            await _cache.RemoveAsync(lockKey);
        }
        #endregion

        #region 商品扫描锁
        public Task<bool> HasScanLock()
        {
           return _cache.GetAsync<bool>(Consts.CacheKey.ScanTaskLock);
        }

        public async Task SetScanLock()
        {
            await _cache.SetAsync(Consts.CacheKey.ScanTaskLock,true);
        }

        public async Task RemoveScanLock()
        {
            await _cache.RemoveAsync(Consts.CacheKey.ScanTaskLock);
        }
        #endregion
    }
}

